<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    /**
     * Binding array data menjadi string tag html <option>
     * untuk digunakan pada view
     * Contoh hasil keluaran
     * <option value='BTH'>Batam - BTH</option>
     * <option value='CGK'>Jakarta - CGK</option>
     * <option value='MES'>Medan - MES</option>
     *
    **/
    function option_encode($array) {
        $return = '';

        foreach ($array as $key => $value) {
            $return .= "<option value='$key'>$value</option>";
        }

        return $return;
    }

    /**
     * Binding array data menjadi string tag html <a href>
     * untuk digunakan pada view
     * Contoh hasil keluaran untuk Slider promo di view
     *
    **/
    function href_encode($array) {
        $return = '';

        foreach ($array as $value) {
            $return .= "<a href='$value->Link' title='$value->Title'>";
            $return .= "<img src='$value->Image' alt='$value->Title->'></a>";
        }

        return $return;
    }

    function search_encode($array) {
        $return = '';

        foreach ($array['hotels'] as $value) {
            //Memanggil data yang diminta dari json adalah sebagai berikut
            $Title          = $value->HotelInfo->HotelName;
            $RatingNumber   = $value->HotelInfo->Rating;
            $LocationName   = $value->HotelInfo->Address;
            $rel            = $value->HotelInfo->AveragePrice;
            $Price          = $this->currency_encode($rel);
            $hotel_code     = $value->HotelInfo->Code;

            $return .=
            "<div class='hotel-wrap clearfix'>
                <div class='hotel-list grid_11'>
                    <a hotel-id='1588 href='#' class='show-hotel' title=''>
                        <img src='img/static/business-hotel-santika-taman-mini-indonesia-indah-jakarta.jpg' class='thumb'>
                    </a>
                    <div class='hotel-desc'>
                        <h2><a hotel-id='1588' href='#' class='show-hotel'>$Title</a></h2>
                        <ul class='hotel-star'>";

            for ($i = 1; $i <=$RatingNumber; $i++)
            {
                $return .= "<li><img src='img/icons/icon-star.png'></li>";
            }
            for ($i = 1; $i <=(5-$RatingNumber); $i++)
            {
                  $return .= "<li><img src='img/icons/icon-star-empty.png'></li>";
            }

            $return .=
                            "</ul>
                        <div class='hotel-location'>
                            $LocationName
                            <span class='map'><a rel='nofollow' href='http://maps.google.com/?output=embed&daddr=-8.7156436,115.1739321&hl=en&z=15' class='show-map'><i class='icon-map-marker'></i>View Map</a></span>
                        </div>
                    </div>
                </div>
                <div class='rate-hotel grid_4'>
                    <div class='hotel-price'>
                        <em>Start from</em>
                        <h3>
                            <span itemprop='price' rel='$rel' class='currency'>$Price</span>
                        </h3>
                    </div>
                    <div class='hotel-action'>
                        <a rel='nofollow' id='view-hotel-1588' class='button purple' href='hotel-details?hotel_code=$hotel_code'>Details</a>
                    </div>
                </div>
            </div>";

        }

        return $return;
    }

    function currency_encode($RawPrice) {
        $Price          = explode(",", $RawPrice);
        $length         = strlen($Price[0]);
        $iterationDiv   = intval($length / 3);

        if ($length%3 == 0) {
            $iterationDiv -=1;
        }

        $lengthIteration = $length - 3;

        for ($i=0; $i < $iterationDiv ; $i++) {
            if ($iterationDiv>0) {
               $Price[0] = substr_replace($Price[0],".",$lengthIteration,0);
            }
            $lengthIteration -= 3;
        }
        return 'IDR '.$Price[0].',00';
    }

    function hotel_found_encode($array) {
        if ($array == null || $array['SearchQueries'] == null) {
            return "<h1><strong class='count'>Hotels Not Found</h1>";
        } else {
            $City               = $array['SearchQueries']->City;
            $NumberHotelFound   = count($array['hotels']);

            return "<h1><strong class='count'>$NumberHotelFound</strong> Hotels Found in <strong>$City</strong>, 2 available</h1>";
        }
    }

    function room_encode($detail_prices) {
        $return = '';

        $market_name        = $detail_prices->Market->MarketName;
        $meal_name          = $detail_prices->Meal->MealName;
        $price_per_night    = $detail_prices->Price->PricePerNight;
        $price_total        = $detail_prices->Price->Total;
        $price_total_str    = $this->currency_encode($price_total);
        $room_bed           = $detail_prices->Room->Bed;
        $room_id            = $detail_prices->Room->IdRoom;
        $room_type          = $detail_prices->Room->RoomType;
        $status             = $detail_prices->Status->StatusName;

        $return .=
        "<hr>
        <div class='room-wrapper clearfix'>
            <div class='room-list grid_6'>
                <a room-id='1588' href='img/static/room-deluxe-big.jpg' class='show-room zoom' title='$room_type Room'>
                    <img src='img/static/room-deluxe.jpg' class='thumb'>
                    <span class='zoom-overlay'>&nbsp;</span>
                </a>
                <div class='room-desc'>
                    <h2><a room-id='1588' href='#' id='deluxe'  class='show-room'>$room_type</a></h2>
                    <div class='room-added'>Includes: $meal_name
                        <a href='#' tooltip='<strong>Refund Policy</strong><ul><li>Cancellation submitted within (3) days before arrival date, will incur full period charge</li><li>Cancellation submitted within (7) days before arrival date, will incur first night charge</li></ul>'>View Promo Policy</a>
                    </div>
                </div>
            </div>
            <div class='rate-room grid_8'>
                <h3>
                    <span itemprop='price' rel='362295.00' class='currency'>$price_total_str</span>
                </h3>
                <div content='in-stock' itemprop='availability' class='only'>$status</div>
                <div class='room-action'>
                    <a rel='nofollow' id='add-room-1588' class='button' href='#'><i class='icon-shopping-cart'></i>Add</a>
                    <a rel='nofollow' id='pay-room-1588' class='button purple' href='hotel-payment-details'>Pay Now</a>
                </div>
            </div>
        </div>";

        return $return;
    }

    function get_detail_prices($array) {
        $return = array();

        foreach ($array['hotels'] as $value) {
            $hotel_code             = $value->HotelInfo->Code;
            $detail_prices          = $value->DetailPrices[0]->Prices;
            $return["$hotel_code"]  = $detail_prices;
        }

        return $return;
    }

    function hotel_payment_encode($hotel_name, $tot_adult, $tot_children, $tot_room, $tot_night, $checkin_date, $checkout_date, $hotel_price) {
        $adult      = $tot_adult > 1 ? $tot_adult . " Adults" : $tot_adult . " Adult";
        $children   = $tot_children > 1 ? $tot_children . " Children" : $tot_children . " Child";
        $room       = $tot_room > 1 ? $tot_room . " Rooms" : $tot_room . " Room";
        $night      = $tot_night > 1 ? $tot_night . " Nights" : $tot_night . " Night";

        $checkin    = date( 'd F Y', strtotime( $checkin_date) );
        $checkout   = date( 'd F Y', strtotime( $checkout_date) );

        $price_str  = $this->currency_encode($hotel_price);

        return
        "<div class='my-order'>
            <h2>$hotel_name</h2>
            <div class='detail-depart'>
                <p class='place'><i class='icon-map-marker'></i>$checkin <img src='img/icons/icon-arrow-to.png' alt='to'> $checkout for $night</p>
                <p class='user'><i class='icon-user'></i>$adult, $children for $room</p>
            </div>
        </div>
        <div class='total-flight'>
            <span class='tot'>TOTAL <em><sup>*</sup>Before tax &amp; service charge</em></span>
            <strong class='price'><span class='currency' rel='$hotel_price'>$price_str</span></strong>
        </div>";
    }

    function adult_form_encode($tot_adult) {
        $return = '';

        for ($i = 1; $i <= $tot_adult; $i++) {
            $return .=
            "<div class='cols clearfix'>
                <p class='left grid_8'>
                    <label for='title-adult[]'>Title<span class='note-required'>*</span></label><br>
                    <select id='title-adult$i' name='title-adult[]'>
                        <option value='Mr'>Mr.</option>
                        <option value='Mrs'>Mrs.</option>
                        <option value='Ms'>Ms.</option>
                    </select>
                </p>
                <p class='right grid_8 prefix_1'>
                    <label for='id-adult[]'>ID Card Number (KTP /SIM /Student Card)<span tooltip='Must be in digit' class='star-notice'></span></label><br>
                    <input type='text' id='id-adult$i' name='id-adult[]' value=''>
                </p>
            </div>
            <div class='cols clearfix'>
                <p class='left grid_8'>
                    <label for='first-name-adult[]'>First Name<span class='note-required'>*</span></label><br>
                    <input type='text' id='first-name-adult$i' name='first-name-adult[]' value=''>
                </p>
                 <p class='right grid_8 prefix_1'>
                    <label for='last-name-adult[]'>Last Name</label><br>
                    <input type='text' id='last-name-adult$i' name='last-name-adult[]' value=''>
                </p>
            </div>";
        }

        if ($tot_adult > 0) {
            return
            "<div class='adult-passenger'>
                <h4>Adult</h4>
                $return
            </div>";
        } else {
            return $return;
        }
    }

    function children_form_encode($tot_children) {
        $return = '';

        for ($i = 1; $i <= $tot_children; $i++) {
            $return .=
            "<div class='cols clearfix'>
                <p class='left grid_8'>
                    <label for='age-child[]'>Age<span class='note-required'>*</span></label><br>
                    <select id='age-child$i' name='age-child[]'>
                        <option selected='selected' value='2'>2</option>
                        <option value='3'>3</option>
                        <option value='4'>4</option>
                        <option value='5'>5</option>
                        <option value='6'>6</option>
                        <option value='7'>7</option>
                        <option value='8'>8</option>
                        <option value='9'>9</option>
                        <option value='10'>10</option>
                        <option value='11'>11</option>
                        <option value='12'>12</option>
                        <option value='13'>13</option>
                        <option value='14'>14</option>
                        <option value='15'>15</option>
                        <option value='16'>16</option>
                        <option value='17'>17</option>
                        <option value='18'>18</option>
                    </select>
                </p>
            </div>
            <div class='cols clearfix'>
                <p class='left grid_8'>
                    <label for='first-name-child[]'>First Name<span class='note-required'>*</span></label><br>
                    <input type='text' id='first-name-child$i' name='first-name-child[]' value=''>
                </p>
                 <p class='right grid_8 prefix_1'>
                    <label for='last-name-child[]'>Last Name</label><br>
                    <input type='text' id='last-name-child$i' name='last-name-child[]' value=''>
                </p>
            </div>";
        }

        if ($tot_children > 0) {
            return
            "<div class='adult-passenger'>
                <h4>Children</h4>
                $return
            </div>";
        } else {
            return $return;
        }
    }

    function booking_encode($detail_booking) {
        $return = '';

        $booking_code            = $detail_booking->BookingCode;
        $checkin_date            = $detail_booking->CheckinDate;
        $checkout_date           = $detail_booking->CheckoutDate;
        $hotel_name              = $detail_booking->HotelName;
        $max_confirmation_date   = $detail_booking->MaxConfirmationDate;
        $nota_number             = $detail_booking->NotaNumber ;
        $status                  = $detail_booking->Status;
        $total_price             = $detail_booking->TotalPrice;

        return $detail_booking;
    }

     function get_details_hotel($array) {

        $return = '';
        $datakiri ='';
        $overview = '';
        $namahotel ='';
        $data=array();

            foreach ($array as $key => $value) {
                $data[$key] = $value;
            }

            $address                 = $data['Address'];
            $telephone               = $data['Telephone'];
            $description             = $data['Description'];
            $hotelname               = $data['HotelName'];

            //untuk memprint alamat dan nomor telepon
            $datakiri .=
             "<div class='poi-attributes'>
                <ul>
                    <li><strong>Address</strong>
                      $address
                    </li>
                <hr>
                    <li><strong>Phone</strong>
                      $telephone
                    </li>
                <hr>
                    <li><h5>More information...<h5></li>
                    <li><i class='icon-globe'></i><a href='#'>View website</a></li>
                    <li><i class='icon-envelope'></i><a href='#'>Send email</a></li>
                </ul>
            </div>";

            //untuk memprint nama dan alamat hotel
            $namahotel .=
            "<hgroup>
                <h1>$hotelname</h1>
                <h4>$address</h4>
            <hgroup>";

            //untuk memprint description
            $overview .=
                "<div class='overview'>
                    <h2>$hotelname Hotel Overview</h2>
                    <div class='overview-content'>
                        <div class='overview-content'>
                            $description
                        </div>
                    </div>
                </div>";

            $return         = array('Datakiri'=> $datakiri,'Overview'=> $overview,'Namahotel' =>$namahotel);

        return $return;
    }

}
